package cn.iocoder.yudao.module.picturebook.controller.admin.bookgather;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.bookgather.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.bookgather.BookGatherDO;
import cn.iocoder.yudao.module.picturebook.service.bookgather.BookGatherService;

@Tag(name = "管理后台 - 绘本集合")
@RestController
@RequestMapping("/picturebook/book-gather")
@Validated
public class BookGatherController {

    @Resource
    private BookGatherService bookGatherService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本集合")
    @PreAuthorize("@ss.hasPermission('picturebook:book-gather:create')")
    public CommonResult<Integer> createBookGather(@Valid @RequestBody BookGatherSaveReqVO createReqVO) {
        return success(bookGatherService.createBookGather(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本集合")
    @PreAuthorize("@ss.hasPermission('picturebook:book-gather:update')")
    public CommonResult<Boolean> updateBookGather(@Valid @RequestBody BookGatherSaveReqVO updateReqVO) {
        bookGatherService.updateBookGather(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本集合")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:book-gather:delete')")
    public CommonResult<Boolean> deleteBookGather(@RequestParam("id") Integer id) {
        bookGatherService.deleteBookGather(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本集合")
                @PreAuthorize("@ss.hasPermission('picturebook:book-gather:delete')")
    public CommonResult<Boolean> deleteBookGatherList(@RequestParam("ids") List<Integer> ids) {
        bookGatherService.deleteBookGatherListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本集合")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:book-gather:query')")
    public CommonResult<BookGatherRespVO> getBookGather(@RequestParam("id") Integer id) {
        BookGatherDO bookGather = bookGatherService.getBookGather(id);
        return success(BeanUtils.toBean(bookGather, BookGatherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本集合分页")
    @PreAuthorize("@ss.hasPermission('picturebook:book-gather:query')")
    public CommonResult<PageResult<BookGatherRespVO>> getBookGatherPage(@Valid BookGatherPageReqVO pageReqVO) {
        PageResult<BookGatherDO> pageResult = bookGatherService.getBookGatherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookGatherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本集合 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:book-gather:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBookGatherExcel(@Valid BookGatherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookGatherDO> list = bookGatherService.getBookGatherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本集合.xls", "数据", BookGatherRespVO.class,
                        BeanUtils.toBean(list, BookGatherRespVO.class));
    }
    //根据分类id查询
    @GetMapping("/get-by-category-id")
    @Operation(summary = "根据分类id查询")
    public CommonResult<List<BookGatherRespVO>> getBookGatherByCategoryId(@RequestParam("categoryId") Integer categoryId) {
        List<BookGatherDO> bookGatherList = bookGatherService.getBookGatherByCategoryId(categoryId);
        return success(BeanUtils.toBean(bookGatherList, BookGatherRespVO.class));
    }
}